//
//  YHUniqueID.m
//  YHTool
//
//  Created by 98du on 2021/11/30.
//

#import "YHUniqueID.h"

@implementation YHUniqueID


/// 创建一个ID
+ (NSString *)yh_timeMillisecondID{
    static NSInteger rotateId = 0;
    static NSInteger nodeId = 0;

    static NSInteger rotateIdWidth = 15;
    static NSInteger rotateIdMask = 0x7FFF;

    static NSInteger nodeIdWidth = 6;
    static NSInteger nodeIdMask = 0x3F;

    static NSInteger T201801010000 = 1514736000000L;

    rotateId = (rotateId + 1)&rotateIdMask;
    
    NSInteger timeTemp = [[NSDate date] timeIntervalSince1970]*1000;
    NSInteger uid = timeTemp - T201801010000;
    uid <<= nodeIdWidth;
    uid += (nodeId & nodeIdMask);

    uid <<= rotateIdWidth;
    uid += rotateId;
    
    return @(uid).stringValue;
}

+ (NSInteger)yh_timeTempID{
    static NSInteger rotateId = 0;
    NSInteger timeTemp = [[NSDate date] timeIntervalSince1970];
    rotateId = rotateId + 1;
    timeTemp = timeTemp + rotateId;
    return timeTemp;
}
+ (NSString *)yh_timeID{
    return @([YHUniqueID yh_timeTempID]).stringValue;
}


@end
